package com.javapapers.java.gcm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

@WebServlet("/GCMNotification")
public class GCMNotification extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// Put your Google API Server Key here
	private static final String GOOGLE_SERVER_KEY = "AIzaSyCYxJrBLRYnpaJieTBVZ3jD82jJajlQEIw";
	static final String REGISTER_NAME = "name";
	static final String MESSAGE_KEY = "message";
	static final String TO_NAME = "toName";
	static final String USER_NAME = "userName";
	static final String TRACK_ID = "trackId";
	static final String TRACK_NAME = "trackName";
	static final String ARTIST_NAME = "artistName";
	static final String REG_ID_STORE = "D:\\GCMRegId.txt";

	public GCMNotification() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		String action = request.getParameter("action");

		// if ("shareRegId".equalsIgnoreCase(action)) {
		//
		// writeToFile(request.getParameter("name"), request.getParameter("regId"));
		// request.setAttribute("pushStatus", "GCM Name and corresponding RegId Received.");
		// request.getRequestDispatcher("index.jsp").forward(request, response);
		//
		// } else
		if ("sendMessage".equalsIgnoreCase(action)) {

			try {
				String fromRegId = request.getParameter(REGISTER_NAME);
				String toRegId = request.getParameter(TO_NAME);
				String userMessage = request.getParameter(MESSAGE_KEY);
				String userName = request.getParameter(USER_NAME);
				String trackId = request.getParameter(TRACK_ID);
				String trackName = request.getParameter(TRACK_NAME);
				String artistName = request.getParameter(ARTIST_NAME);
				Sender sender = new Sender(GOOGLE_SERVER_KEY);
				Message message = new Message.Builder().timeToLive(30).delayWhileIdle(true).addData(USER_NAME, userName).addData(
						TRACK_ID, trackId).addData(TRACK_NAME, trackName).addData(ARTIST_NAME, artistName).addData(
						REGISTER_NAME, "fromName").build();
				// Map<String, String> regIdMap = readFromFile();
				String regId = toRegId;
				Result result = sender.send(message, regId, 1);
				request.setAttribute("pushStatus", result.toString());
			} catch (IOException ioe) {
				ioe.printStackTrace();
				request.setAttribute("pushStatus", "RegId required: " + ioe.toString());
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("pushStatus", e.toString());
			}
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} else if ("multicast".equalsIgnoreCase(action)) {

			try {
				String fromName = request.getParameter(REGISTER_NAME);
				String userMessage = request.getParameter(MESSAGE_KEY);
				Sender sender = new Sender(GOOGLE_SERVER_KEY);
				Message message = new Message.Builder().timeToLive(30).delayWhileIdle(true).addData(MESSAGE_KEY,
						userMessage).addData(REGISTER_NAME, fromName).build();
				// Map<String, String> regIdMap = readFromFile();

				List<String> regIdList = new ArrayList<String>();

				// RestTemplate restTemplate = new RestTemplate();
				// List<LinkedHashMap> emps = restTemplate.getForObject("http://localhost:8080/MFWebService"
				// + UserRestURIConstants.GET_ALL_USER, List.class);
				// List<Kullanici> kullanicis = new ArrayList<Kullanici>();
				// for (LinkedHashMap map : emps) {
				// regIdList.add((String) map.get("gcmRegId"));
				// }
				// for (Entry<String, String> entry : regIdMap.entrySet()) {
				// regIdList.add(entry.getValue());
				// }

				MulticastResult multiResult = sender.send(message, regIdList, 1);
				request.setAttribute("pushStatus", multiResult.toString());
			} catch (IOException ioe) {
				ioe.printStackTrace();
				request.setAttribute("pushStatus", "RegId required: " + ioe.toString());
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("pushStatus", e.toString());
			}
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}

	// private void writeToFile(String name, String regId) throws IOException {
	// Map<String, String> regIdMap = readFromFile();
	// regIdMap.put(name, regId);
	// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(REG_ID_STORE, false)));
	// for (Map.Entry<String, String> entry : regIdMap.entrySet()) {
	// out.println(entry.getKey() + "," + entry.getValue());
	// }
	// out.println(name + "," + regId);
	// out.close();
	//
	// }
	//
	// private Map<String, String> readFromFile() throws IOException {
	// BufferedReader br = new BufferedReader(new FileReader(REG_ID_STORE));
	// String regIdLine = "";
	// Map<String, String> regIdMap = new HashMap<String, String>();
	// while ((regIdLine = br.readLine()) != null) {
	// String[] regArr = regIdLine.split(",");
	// regIdMap.put(regArr[0], regArr[1]);
	// }
	// br.close();
	// return regIdMap;
	// }
}
